package util.methods;
/** 
 * Time and date methods
 * Use getTime for the starting time to be passed getElapsedTimeStr
 */

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class TimeHelpers 
{
	// called by assembler and annotator
    static public String getDate ( )
    {
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM-yy HH:mm:ss"); 
        return sdf.format(date);
    }
    
    static public String getDateOnly ( )
    {
        Date date=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("dd-MMM-yy"); 
        return sdf.format(date);
    }
    
    static public String convertDate(String date) { // yyyy-dd-mm
   		int first = date.indexOf("-");
		int last = date.lastIndexOf("-");
		String yr = date.substring(0, first);
		yr = yr.substring(2); // remove 20
		String mn = date.substring(first+1, last);
		String dy = date.substring(last+1);
        return dy + "-" + TimeHelpers.getMonth(mn) + "-" + yr;
    }
    static public String getMonth(String mn) {
     	HashMap<String, String> hMap = new HashMap<String, String>();
       	hMap.put("01", "Jan"); hMap.put("02", "Feb"); hMap.put("03", "Mar");
       	hMap.put("04", "Apr"); hMap.put("05", "May"); hMap.put("06", "Jun");
       	hMap.put("07", "Jul"); hMap.put("08", "Aug"); hMap.put("09", "Sep");
       	hMap.put("10", "Oct"); hMap.put("11", "Nov"); hMap.put("12", "Dec");
       	return hMap.get(mn);
    }
    
    static public String longDate(long l) {
    		Date date = new Date(l);
    		return date.toString();
    }
    
    static public long getTime () {
    		return System.currentTimeMillis(); 
    }
	
	static public String getElapsedTimeStr(long t) 	{
		long et = System.currentTimeMillis() - t;
		return getElapsedTimeStrFromInterval(et);
	}
	static public String getElapsedTimeStrFromInterval(long et) 	{
		et /= 1000F;
		long min = et / 60;
		long sec = et % 60;
		long hr = min / 60;
		min = min % 60;
		long day = hr / 24;
		hr = hr % 24;
		String str = " ";
		if (day > 0) str += day + "d:";
		if (hr > 0 ) str += hr + "h:";
		str += min + "m:" + sec + "s";
		return str;
	}
	
	static public void printMemoryUsed() {
	    Runtime runtime = Runtime.getRuntime();
	    // Run the garbage collector
	    runtime.gc();
	    double memory = runtime.totalMemory() - runtime.freeMemory();
	    String mb = String.format("%6.2f", (memory/(1024.0 * 1024.0)));
	    System.out.println("Used memory: " + mb + " megabytes");
	}
}
